<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-2-25
 * Time: 下午8:57
 */

namespace backend\controllers;


use common\models\Contact;
use common\models\Content;
use common\models\Menu;
use common\service\CommonService;
use common\service\ConstantService;
use common\service\LogaddService;
use common\service\MenuService;
use common\service\UeditorService;

class ContentController extends BaseController
{

    /**
     * 列表页
     */
    public function actionContent(){
        $query=Content::find();
        if (!empty($this->get("title"))){
            $query->andWhere(['like','title',$this->get("title")]);
        }
        $p = intval( $this->get('page',1) );
        $this->page_size = intval( $this->get('limit',10) );
        $p = ( $p > 0 )? $p : 1;
        $pages = CommonService::ipagination( [
            'total_count' => $query->count(),//总条数
            'page_size' => $this->page_size,
            'page' => $p,
            'display' => 10
        ]);
        $offset = ($p - 1) * $this->page_size;
        $data=$query->limit($this->page_size)->offset($offset)->orderBy('create_time desc')->asArray()->all();
        return $this->render('index',[
            'datas'=>$data,
            'title'=>$this->get("title"),
            'pages' => $pages,
            'url' =>'/content/content',
            'search_conditions' => ['title'=>$this->get("title")]]);
    }


    /**
     * 删除文章
     */
    public function actionDel(){
            $id=$this->post('id');
            $content=Content::find()->where(['id'=>$id])->one();
           $re= $content->delete();
        if($re){
            $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'删除文章'.$content['title'];
            LogaddService::logAdd($this->admin['username'],$str);
            return $this->renderJson([],'删除成功',ConstantService::$response_success);
        }else{
            return $this->renderJson([],'删除失败',ConstantService::$response_fail);
        }
    }

    /**
     * 根据id显示要修改文章内容
     */
    public function actionUpdate(){

        $id=$this->get('id');
        $select=Content::find()->where(['id'=>$id])->one();
        return $this->render('updata',['data'=>$select]);
    }
    //修改文章
    public function actionUpdateindex(){
        if ($this->isAjax() && $this->post()){
            if ($this->post('title') && $this->post('content') &&  $this->post('type')){
                $content=$this->post('title');
                $id=$this->post('id');
                $result=Content::find()->where(["id"=>"$id"])->one();        //返回一条
                if ($result){
                    $result->setAttributes(['title'=>$this->post('title') ,'content'=>$this->post('content'),'type'=>$this->post('type')]);
                    $save=$result->save();
                    if($save){
                        $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'修改文章'.$content;
                        LogaddService::logAdd($this->admin['username'],$str);
                        return $this->renderJson([],"修改成功",ConstantService::$response_success);
                    }else{
                        return $this->renderJson([],"修改失败，请重新修改",ConstantService::$response_fail);
                    }
                }else{
                    return $this->renderJson([],"为查询到需要修改的数据",ConstantService::$response_fail);
                }
            }else{
                return $this->renderJson([],"请填写完标题和内容再次修改",ConstantService::$response_fail);
            }
        }
    }


    public function actionSelect(){
        $id=$this->get('id');
        $select=Content::find()->where(['id'=>$id])->one();
        return $this->render('select',['data'=>$select]);
    }

    public function actionAdd(){
        $toolbars=UeditorService::getToolbars();
        return $this->render('add',['toolbars'=>$toolbars]);
    }



    public function actionAddindex(){

        $content=new Content();
        if($this->isAjax() && $this->post()){
            if ($this->post('title') && $this->post('content') &&  $this->post('type')){
                $content->setAttributes(['title'=>$this->post('title'),'content'=>$this->post('content') ,'type'=>$this->post('type') ,'create_time'=>time()]);
                $data=$content->save();
                if($data){
                    $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'添加文章'.$content['title'];
                    LogaddService::logAdd($this->admin['username'],$str);
                    return $this->renderJson([],"添加成功",ConstantService::$response_success);
                }else{
                    return $this->renderJson([],"添加失败",ConstantService::$response_fail);
                }
            }else{
                    return $this->renderJson([],"添加失败,标题内容等不能为空",ConstantService::$response_fail);
            }

        }
    }

    /**
     * 联系方式
     * @return string
     */
    public function actionContact(){
        if($this->isAjax() && $this->post()){
            $phone = $this->post('phone');
            $admin = Contact::findOne(['id'=>1]);
            if(!$admin){
                $db = new Contact();
                $db->setAttributes(['phone'=>$phone]);
                $db->save();
                return $this->renderPageJson('设置成功!','',ConstantService::$response_tab_success);
            }
            $admin->phone = $phone;
            if($admin->save()){
                return $this->renderPageJson('设置成功!','',ConstantService::$response_tab_success);
            }
        }
        $data = Contact::find()->where(['id'=>1])->asArray()->one();
        $phone = $data['phone'];
        return $this->render('contact',['phone'=>$phone]);
    }

}